using System;
using System.Collections;
using System.Collections.Generic;
using NettyDeomo.entity;
using NettyDeomo.msg;
using UnityEngine;

public class CubeCtl : MonoBehaviour
{
    private Player player;

    private Vector2 preDirection = Vector2.zero;

    private Vector2 nowDirection = Vector2.zero;
    // Start is called before the first frame update
    void Start()
    {
        player = GetComponent<Player>();
    }
    
    
    // Update is called once per frame
    void Update()
    {
        if (!player.isMainPlayer)
        {
            return;
        }

        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        nowDirection.x = Math.Abs(h)<0.1?0:(h > 0 ? 1 : -1);
        nowDirection.y = Math.Abs(v)<0.1?0:(v > 0 ? 1 : -1);
        //TODO 玩家操作过快问题，记录上一次发送的移动指令，当前移动指令应该和上一次发送的移动指令进行比较

        if (nowDirection.Equals(preDirection))
        {
            
            return;
        }
        Debug.Log("变向移动，发给服务器！");
        if (nowDirection.x != player.moveDir.x || nowDirection.y != player.moveDir.z)
        {
            player.isMoving = true;
        }

        player.moveDir.x = nowDirection.x;
        player.moveDir.z = nowDirection.y;
        
        preDirection.x = nowDirection.x;
        preDirection.y = nowDirection.y;

        //发送网络请求
        if (NettyClient.Instance.isRunning )
        {
            C2S_Move move = new C2S_Move();
            move.x = nowDirection.x;
            move.y = nowDirection.y;
            move.speed = player.speed;
            NettyClient.Instance.sendMessage(move);
            //发送网络移动请求
            return;
        }

        // Vector3 direction = new Vector3(h, 0, v);
        // player.localMove(direction);
    }
    
}
